<template>
  <van-popup
    :value="value"
    @click-close-icon="$emit('input', false)"
    closeable
    position="left"
  >
    <div class="article-channel">
      <div class="head">
        <h3>我的频道<small>点击进入频道</small></h3>
        <a class="edit" href="javascript:;" @click='isShow=true'>编辑</a>
      </div>
      <div class="body">
        <!-- <a href="javascript:;" class="active">推荐</a> -->
        <a href="javascript:;" v-for="(item,index) in myChannels" :key="item.id"  :class="{active:active===index}"  @click="enterChannel(index)">{{
          item.name
        }} <van-icon name="close"  v-show='isShow' @click="isShow=false"/></a>
      </div>
      <div class="head" style="margin-top: 12px">
        <h3>频道推荐</h3>
      </div>
      <div class="body">
        <!-- <a href="javascript:;" v-for="item in remnantChannels" :key="item.id" @click="addChannel(item)"
          >+ {{ item.name }}</a
        > -->
        <a href="javascript:;" v-for="item in remnantChannels" :key="item.id">+ {{ item.name }} </a
        >

      </div>
    </div>
  </van-popup>
</template>
<script>
import { getAllChannels } from '@/api/article'
export default {
  name: 'ArticleChannel',
  data () {
    return {
      allChannels: [],
      isShow: false
    }
  },
  props: {
    value: {
      type: Boolean,
      default: false
    },
    myChannels: {
      type: Array
      // default: () => []
    },
    active: {
      type: Number,
      default: () => {}
    }
  },
  methods: {
    // 点击每一个我的频道
    enterChannel (index) {
      // 传出去一个index,确定跳转到那个界面
      this.$emit('indexOperation', index)
      // 关闭频道页面
      // this.$emit('input', false)
    }
    // addChannel (item) {
    //   this.myChannels.push(item)
    // }
  },
  async created () {
    // 获取所有频道
    const res = await getAllChannels()
    // console.log(res.data.channels)
    this.allChannels = res.data.channels
  },
  computed: {
    // 获取的所有频道里边筛选出my的剩余的即是可选的
    remnantChannels () {
      console.log(
        this.allChannels.filter(
          (item) => !this.myChannels.find((item1) => item1.id === item.id)
        )
      )
      return this.allChannels.filter(
        (item) => !this.myChannels.find((item1) => item1.id === item.id)
      )
    }
  }
}
</script>
<style scoped lang="less">
.van-popup {
  width: 100%;
  height: 100%;
  ::v-deep .van-popup__close-icon {
    font-size: 20px;
    right: 12px;
    top: 12px;
  }
  .article-channel {
    margin-top: 44px;
    .head {
      padding: 0 16px;
      display: flex;
      justify-content: space-between;
      justify-items: center;
      padding-bottom: 12px;
      h3 {
        font-size: 16px;
        color: #333;
        margin: 0;
        small {
          font-size: 12px;
          color: #999;
          margin-left: 10px;
        }
      }
      .edit {
        float: right;
        height: 22px;
        width: 52px;
        line-height: 22px;
        text-align: center;
        color: #de644b;
        border-radius: 11px;
        border: 1px solid #de644b;
        font-size: 12px;
      }
    }
    .body {
      padding: 0 6px 0 16px;
      a {
        position:relative;
        display: inline-block;
        padding: 0 8px;
        font-size: 14px;
        color: #3a3948;
        background: #f7f8fa;
        height: 36px;
        line-height: 36px;
        min-width: 78px;
        margin-right: 10px;
        margin-bottom: 12px;
        border-radius: 18px;
        text-align: center;
        .van-icon {
        position: absolute;
        top: -5px;
        right: -8px;
        color:red;
      }
      }
      .active {
        color: #fff;
        background: #de644b;
      }
    }
  }
}
.article-channel {
  margin-top: 44px;
}
</style>
